<template>
	<view>
		<view class="goshop-pay-wrap">
			<u-toast ref="uToast"></u-toast>
			<view class="lgm-font-size font-bold text-color-primary">{{moneyText}}{{orderData.amount}}</view>
			<view class="u-m-t-10" v-if="locale ==='zh-Hans'">{{afterorderingText}}<text class="lgm-font-size font-bold text-color-primary">{{min}}{{minuteText}} {{sec}}{{secondText}} </text>{{finishText}}</view>
			<view class="u-m-t-10 row-cen-cen" v-else>
			    <view class="row-cen-cen">
					{{afterorderingText}}
				</view>
				<text class="lgm-font-size font-bold text-color-primary row-cen-cen">{{min}}{{minuteText}} {{sec}}{{secondText}} </text>
			    <view class="row-cen-cen">
					{{finishText}}
				</view>
			</view>
		</view>
		<view class="goshop-list-wrap">
			<u-radio-group
				v-model="form.payment"
				placement="column"
				iconPlacement="right"
			>
				<view class="goshop-list-item goshop-list-item-bottom" v-if="locale ==='zh-Hans'">
					<view class="goshop-list-item-left">
						<view class="iconfont iconfont-wallet icon-img-24"></view><view class="u-m-l-10">{{walletpayText}}</view>
					</view>
					<view class="goshop-list-item-right">
							<u-radio
								key="1"
								name="1"
							>
							</u-radio>
					</view>
				</view>
				<view class="goshop-list-item goshop-list-item-bottom" v-if="locale ==='zh-Hans'">
					<view class="goshop-list-item-left">
						<view class="iconfont iconfont-zfb icon-img-24"></view><view class="u-m-l-10">{{zfbpayText}}</view>
					</view>
					<view class="goshop-list-item-right">
						<u-radio
							key="2"
							name="2"
						>
						</u-radio>
					</view>
				</view>
				<view class="goshop-list-item goshop-list-item-bottom" v-if="locale ==='zh-Hans'">
					<view class="goshop-list-item-left">
						<view class="iconfont iconfont-wxpay icon-img-24"></view><view class="u-m-l-10">{{wxpayText}}</view>
					</view>
					<view class="goshop-list-item-right">
						<u-radio
							key="3"
							name="3"
						>
						</u-radio>
					</view>
				</view>
				<view class="goshop-list-item goshop-list-item-bottom">
					<view class="goshop-list-item-left">
						<view class="iconfont iconfont-paypal icon-img-24"></view><view class="u-m-l-10">{{paypalpayText}}</view>
					</view>
					<view class="goshop-list-item-right">
						<u-radio
							key="4"
							name="4"
						>
						</u-radio>
					</view>
				</view>
			</u-radio-group>
		</view>
		<view class="goshop-pay-submit">
			<u-button type="error" @click="submitPay"  :text="confirmpaymentText"></u-button>
		</view>
	</view>
</template>

<script>
	import {orderPay,prePay,orderInfo,preInfo} from '@/common/http.api.js'
	import {isEmpty} from '@/utils/utils';
	import config from '@/common/config.js'
	export default {
		data() {
			return {
				form:{
					id:'',
					platform:'app',
					payment:'4',
				},
				payData:{},
				orderData:{
					amount:0.00,
				},
				day:0,
				hr:0,
				min:0,
				sec:0,
				alipayerrText:'',
				callalipayText:'',
				paypalerrText:'',
				afterorderingText:'',
				minuteText:'',
				zfbpayText:'',
				wxpayText:'',
				callwxText:'',
				secondText:'',
				walletpayText:'',
				finishText:'',
				paypalpayText:'',
				callpaypalText:'',
				wxerrText:'',
				confirmpaymentText:'',
				newText:'',
				moneyText:'',
				locale:'',
				paySuccessText:'',
				payFailText:'',
			};
		},
		async onLoad(e){
			this.form.id = e.id;
			await this.getOrderInfo();
		},
		onShow(){
		   this.refresh();
		   this.locale = this.$i18n.locale;
		},
		methods:{
			refresh(){
				this.alipayerrText = this.$t('common.alipayerr.text');
				this.callalipayText = this.$t('common.callalipay.text');
				this.paypalerrText = this.$t('common.paypalerr.text');
				this.afterorderingText = this.$t('orderpay.afterordering.text');
				this.minuteText = this.$t('orderpay.minute.text');
				this.zfbpayText = this.$t('common.zfbpay.text');
				this.wxpayText = this.$t('common.wxpay.text');
				this.callwxText = this.$t('common.callwx.text');
				this.secondText = this.$t('orderpay.second.text');
				this.walletpayText = this.$t('common.walletpay.text');
				this.finishText = this.$t('orderpay.finish.text');
				this.paypalpayText = this.$t('common.paypalpay.text');
				this.callpaypalText = this.$t('common.callpaypal.text');
				this.wxerrText = this.$t('common.wxerr.text');
				this.confirmpaymentText = this.$t('orderpay.confirmpayment.text');
				this.newText = this.$t('orderpay.new.text');
				this.moneyText = this.$t('common.money.text');
				
				this.paySuccessText = this.$t('common.pay.success');
				this.payFailText = this.$t('common.pay.fail');
			},
			 countdown () {
				  let end = Date.parse(this.orderData.createTime)
				  let endDate = new Date(end);
				  endDate.setMinutes(endDate.getMinutes()+9);
				  const now = Date.parse(new Date())
				  const msec = endDate - now
				  if(msec<0) return;
				  let day = parseInt(msec / 1000 / 60 / 60 / 24)
				  let hr = parseInt(msec / 1000 / 60 / 60 % 24)
				  let min = parseInt(msec / 1000 / 60 % 60)
				  let sec = parseInt(msec / 1000 % 60)
				  this.day = day
				  this.hr = hr > 9 ? hr : '0' + hr
				  this.min = min > 9 ? min : '0' + min
				  this.sec = sec > 9 ? sec : '0' + sec
				  const that = this
				  if(min>=0 && sec>=0){
					//倒计时结束关闭订单
					if(min==0 && sec==0){
					  return
					}
					setTimeout(function () {
					  that.countdown()
					}, 1000)
				  }
			 },
			getOrderInfo(){
				let that = this;
				preInfo({params:{id:this.form.id}}).then(res => {
					if (res.code === 0) {
						this.orderData = res.data;
						if(this.orderData.status != 0){
							this.$refs.uToast.show({
								message: that.newText,
								type: 'error',
							})
							return;
						}
						this.countdown();
					} else {
						uni.hideLoading();
						this.$refs.uToast.show({
							message: res.msg,
							type: 'error',
						})
					}
				});
			},
			submitPay(){
				let that = this;
				//this.form.payment = parseInt(this.form.payment);
				prePay(this.form).then(res => {
					if (res.code === 0) {
						that.payData = res.data.invokeResponse;
						if(res.data.pay){
							uni.getProvider({  //获取可用的支付环境
								service: 'payment',
								success: res=>{
									// #ifdef H5
									//支付宝
									if('2' == that.form.payment){

									}else if('3' == that.form.payment){
		
									}else if('4' == that.form.payment){
										window.location.href = that.payData.approve; //H5端跳转到paypal支付页面
									}
									// #endif

									// #ifdef APP-PLUS
										//支付宝
										if('2' == that.form.payment){
											//先判断用户是否有支付宝支付环境
											if (~res.provider.indexOf('alipay')) { 
												uni.showLoading({title: that.callalipayText})
											}else{
												uni.showToast({title: that.alipayerrText,icon:'none'})
												return;
											}
											//沙箱支付
											var EnvUtils = plus.android.importClass("com.alipay.sdk.app.EnvUtils");
											EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
											that.payData = that.payData.invokeResponse.body;
										}else if('3' == that.form.payment){
											 //先判断用户是否有微信支付环境
											if (~res.provider.indexOf('wxpay')) {
												uni.showLoading({title: that.callwxText})
											}else{
												uni.showToast({title:that.wxerrText,icon:'none'})
												return;
											}
										}else if('4' == that.form.payment){
											 //先判断用户是否有paypal支付环境
											if (~res.provider.indexOf('paypal')) {
												uni.showLoading({title: that.callpaypalText})
											}else{
												uni.showToast({title: that.paypalerrText,icon:'none'})
												return;
											}
											let orderInfo = {
											  "clientId": that.payData.clientId, //开发者中心clientId
											  "orderId": that.payData.orderId, //订单id
											  "userAction": "continue", //按钮样式 paynow/continue
											  "currency":"USD", //币种
											  "channelId":that.payData.channelId,
											  "environment":that.payData.mode //运行环境 sandbox(沙盒) / live(线上)
											};
											debugger
											that.payData = orderInfo;
										}
										console.info("支付方式：" + res.provider);
										console.info('支付数据：' + JSON.stringify(that.payData));
										// 发起支付
										uni.requestPayment({
										  provider: 'paypal',
										  orderInfo: that.payData, //微信、支付宝订单数据
										  success: (res) => {
											  console.log('success:' + JSON.parse(res.rawdata));
											  uni.hideLoading();
											  this.$refs.uToast.show({
													type: 'success',
													message: that.paySuccessText,
													complete() {
													  uni.redirectTo({
														url: 'pages/common/hint/pay_success?type=order&status=success&id='+that.payData.orderId+'&channelId='+that.payData.channelId
													  });
													}
												})
										  },
										  fail: (res) => {
												uni.hideLoading(); 
												// 支付失败
												// uni.showModal({
												// 	content: "支付失败",
												// 	showCancel: false
												// })
												uni.showModal({
												  content: res.errMsg,
												  showCancel: false
												})
										  },
										})
									// #endif
								}
							});
						}
					} else {
						uni.hideLoading();
						this.$refs.uToast.show({
							message: res.msg,
							type: 'error',
						})
					}
				});
			},

		}
	}
</script>

<style lang="scss">
.goshop-list-item-left{
	display: flex;
	align-items: center;
}
.goshop-pay-wrap{
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
	height: 180rpx;
}
.goshop-pay-submit{
	padding: 20rpx;
}
</style>
